package com.silence.gankio.activity

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.ViewGroup
import com.silence.gankio.R
import com.silence.gankio.mvp.control.HttpControl
import com.silence.gankio.util.Utils
import com.silence.gankio.widget.BaseToolBar
import com.silence.gankio.widget.ToolBarCommView
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_base.*

abstract class BaseActivity : AppCompatActivity(), HttpControl.IHttpView {

    private var mToolbar: BaseToolBar? = null
    private val mCompositeDisposable: CompositeDisposable = CompositeDisposable()

    override fun onCreate(savedInstanceState: Bundle?) {
        if (isSupportTransLucentStatus()) {
            Utils.fitStatusBar(this, false, false, R.color.toolbar_color)
        }
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_base)

        root_view.addView(LayoutInflater.from(this).inflate(getActivityContentView(), root_view, false), 1)
    }

    abstract fun getActivityContentView(): Int

    protected fun isSupportTransLucentStatus(): Boolean {
        return true
    }

    protected fun initToolBar(toolBarCommView: ToolBarCommView?): BaseToolBar {
        if (null == mToolbar) {
            mToolbar = vs_toolbar.inflate() as BaseToolBar
        } else {
            mToolbar!!.removeAllViews()
        }
        mToolbar!!.addView(toolBarCommView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        return mToolbar as BaseToolBar
    }

    override fun finish() {
        Utils.closeKeyBorad(this)
        super.finish()
    }

    override fun applyDisposable(disposable: Disposable) {
        mCompositeDisposable.add(disposable)
    }

    override fun onDestroy() {
        mCompositeDisposable.dispose()
        mCompositeDisposable.clear()
        super.onDestroy()
    }


    override fun onBackPressed() {

        super.onBackPressed()
    }

    override fun onNetError(code: Int, msg: String?) {

    }

    override fun onNetFinish() {

    }

}
